package org.example.mutithread.future;

import com.google.common.util.concurrent.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangjg
 * @desc fl
 * @date 2023/03/30 09:18
 **/
public class FutureListen {

    private static final Logger log = LoggerFactory.getLogger(FutureListen.class);

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        ListeningExecutorService guavaExecutor = MoreExecutors.listeningDecorator(executor);
        ListenableFuture<String> future1 = guavaExecutor.submit(() -> {
            //step 1
            log.info("执行step 1");
            TimeUnit.SECONDS.sleep(3);
            return "step1 result";
        });
        ListenableFuture<String> future2 = guavaExecutor.submit(() -> {
            //step 2
            log.info("执行step 2");
            TimeUnit.SECONDS.sleep(10);
            if (true) {
                throw new RuntimeException("==============");
            }
            return "step2 result";
        });
        ListenableFuture<List<String>> future1And2 = Futures.allAsList(future1, future2);

        Futures.FutureCombiner<List<String>> listFutureCombiner = Futures.whenAllComplete(future1And2);
        /*Futures.addCallback(future1And2, new FutureCallback<List<String>>() {
            @Override
            public void onSuccess(List<String> result) {
                log.info("f1,f2 end {}", result.toString());
                guavaExecutor.shutdown();
                executor.shutdown();
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("e2", t);
                guavaExecutor.shutdown();
                executor.shutdown();
            }
        }, guavaExecutor);*/



        log.info("--------------------------");


    }
}
